package music.Share;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.ClipboardManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ShowFile extends Activity implements OnClickListener{
	/////Klassenvariablen
	private String urlfilename;
	private String weburl;
	private String secordner;
	private String uid;
	private int slashIndex;
	private File filetest;
	private String filetype;
	private boolean isBild=false;
	private Intent intent;
	private final MediaPlayer mediaPlayer=new MediaPlayer();
	private boolean backbuttonpressed=false;
	private String testfilepfad;
	private String aufrufer;
	/////
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void onCreate(Bundle savedInstanceState) 
		{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dateianzeigen);
		
		///Layout-Zugriff
		((Button) findViewById(R.id.copybtn)).setOnClickListener(this);
		((Button) findViewById(R.id.sendbtn)).setOnClickListener(this);
		
		((ImageView) findViewById(R.id.bildvorschau)).setOnClickListener(this);
			
		///Variablen-Initialisierung
		uid = ((TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		intent= getIntent();
		weburl=intent.getStringExtra("selected");
		aufrufer=intent.getStringExtra("aufrufer");
		
		if (aufrufer.equals("Eingang")) ((Button) findViewById(R.id.annehmenbtn)).setVisibility(View.VISIBLE);
		
		((Button) findViewById(R.id.annehmenbtn)).setOnClickListener(this);
		
		slashIndex = weburl.lastIndexOf('/');
		secordner= weburl.substring(54, slashIndex);
		urlfilename = weburl.substring(slashIndex + 1);
		filetype=urlfilename.substring(urlfilename.lastIndexOf("."));
			System.out.println(filetype);
		
		if (filetype.equals(".jpg") || filetype.equals(".jpeg") || filetype.equals(".bmp") || filetype.equals(".tif") || filetype.equals(".tiff") || filetype.equals(".png") || filetype.equals(".gif")) 
				isBild=true;
		
		if (isBild) ((ImageView) findViewById(R.id.bildvorschau)).setVisibility(View.INVISIBLE);
			
		testfilepfad="/data/data/music.Share/"+secordner+urlfilename;
		filetest=new File(testfilepfad);
		
		
		System.out.println(testfilepfad);
	  
		if (!isBild) // /--->>>MUSIK
		{

			if (filetest.exists()) {
				try {
					mediaPlayer
							.setDataSource((new FileInputStream(testfilepfad))
									.getFD());

					mediaPlayer.prepare();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				mediaPlayer.start();
				// ((TextView)(findViewById(R.id.weblink))).setVisibility(View.VISIBLE);
				((Button) findViewById(R.id.copybtn))
						.setVisibility(View.VISIBLE);
				((Button) findViewById(R.id.sendbtn))
						.setVisibility(View.VISIBLE);

			} else {
				new AsyncTask() {
					ProgressBar progress = (ProgressBar) findViewById(R.id.vorschauprg);

					@Override
					protected Object doInBackground(Object... params) {
						Methoden.FTPdownload(secordner, urlfilename, uid);
						return null;
					}

					@Override
					protected void onPostExecute(Object result) {
						super.onPostExecute(result);
						progress.setVisibility(View.INVISIBLE);
						// ((TextView)(findViewById(R.id.weblink))).setVisibility(View.VISIBLE);
						((Button) findViewById(R.id.copybtn))
								.setVisibility(View.VISIBLE);
						((Button) findViewById(R.id.sendbtn))
								.setVisibility(View.VISIBLE);

						try {
							mediaPlayer.setDataSource((new FileInputStream(
									testfilepfad)).getFD());

							mediaPlayer.prepare();
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						if (backbuttonpressed == false)
							mediaPlayer.start();

					}

					@Override
					protected void onPreExecute() {
						super.onPreExecute();
						progress.setVisibility(View.VISIBLE);
					}
				}.execute();
			}
		} else // /---> BILD
		{

			if (filetest.exists()) {
				System.out.println("Bildausgabe offline!!! " + testfilepfad);
				new AsyncTask() {
					ProgressBar progress = (ProgressBar) findViewById(R.id.vorschauprg);
					ImageView bild = (ImageView) findViewById(R.id.bildvorschau);
					Bitmap bitmap;

					@Override
					protected Object doInBackground(Object... params) {
						bitmap = BitmapFactory.decodeFile(testfilepfad);
						return null;
					}

					@Override
					protected void onPostExecute(Object result) {
						super.onPostExecute(result);
						bild.setImageBitmap(bitmap);
						// ((TextView)(findViewById(R.id.weblink))).setVisibility(View.VISIBLE);
						((Button) findViewById(R.id.copybtn)).setVisibility(View.VISIBLE);
						((Button) findViewById(R.id.sendbtn)).setVisibility(View.VISIBLE);

						bild.setVisibility(View.VISIBLE);
						progress.setVisibility(View.INVISIBLE);
					}

					@Override
					protected void onPreExecute() {
						super.onPreExecute();
						progress.setVisibility(View.VISIBLE);
					}

				}.execute();
			} else {
				System.out.println("Bildausgabe ONLINE!!!");

				new AsyncTask() {
					ProgressBar progress = (ProgressBar) findViewById(R.id.vorschauprg);

					@Override
					protected Object doInBackground(Object... params) {
						Methoden.FTPdownload(secordner, urlfilename, uid);
						return null;
					}

					@Override
					protected void onPostExecute(Object result) {
						super.onPostExecute(result);
						ImageView bild = (ImageView) findViewById(R.id.bildvorschau);
						Bitmap bitmap = BitmapFactory.decodeFile(testfilepfad);

						bild.setImageBitmap(bitmap);
						progress.setVisibility(View.INVISIBLE);
						// ((TextView)(findViewById(R.id.weblink))).setVisibility(View.VISIBLE);
						((Button) findViewById(R.id.copybtn)).setVisibility(View.VISIBLE);
						((Button) findViewById(R.id.sendbtn)).setVisibility(View.VISIBLE);

						bild.setVisibility(View.VISIBLE);
					}

					@Override
					protected void onPreExecute() {
						super.onPreExecute();
						progress.setVisibility(View.VISIBLE);
					}
				}.execute();
			}
		}
		((TextView) (findViewById(R.id.filepath))).setText(urlfilename);
		((TextView) (findViewById(R.id.weblink))).setText(weburl);
	}

	@Override
	public void onClick(View v) {
		
		switch( v.getId()) 
		{
		case R.id.copybtn:
			ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
			clipboardManager.setText(((TextView)(findViewById(R.id.weblink))).getText());
			break;
		case R.id.sendbtn:
  			Intent eventsSENDntent= new Intent(this, SendFile.class);
  			eventsSENDntent.putExtra("ordner",secordner);
  			eventsSENDntent.putExtra("filename",urlfilename);
			startActivity(eventsSENDntent);
			break;
		case R.id.bildvorschau:
			Intent eventspicIntent= new Intent(this, BigPicture.class);
			eventspicIntent.putExtra("filename",testfilepfad);
			startActivity(eventspicIntent);
			break;
		case R.id.annehmenbtn:
			Methoden.OrdnerFreigeben(secordner,uid);
			setResult(Activity.RESULT_OK);
			((Button) findViewById(R.id.annehmenbtn)).setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}
		

		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
		{
		if (keyCode == KeyEvent.KEYCODE_BACK) 
			{
			if (mediaPlayer.isPlaying()) mediaPlayer.stop();
			ShowFile.this.finish();
			backbuttonpressed=true;
			return true;
			}
		return super.onKeyDown(keyCode, event);
		}
	
    @Override //-> OnRotate
    public void onConfigurationChanged(Configuration newConfig)
    	{
        super.onConfigurationChanged(newConfig);
        System.out.println("Bildschirm gedreht!");
    	}

	
}